<template>
  <div class="forget-pass-page">
    <img class="background-img" src="../image/loginBg.png" />
    <div class="login-bg">
      <!--      <div class="login-title">政校会易登录</div>-->

      <div class="title-container">
        <span class="title">忘记密码</span>
      </div>
      <div class="title-line" />

      <el-form
        ref="loginForm"
        :model="forgetForm"
        class="login-form"
        label-position="left"
      >
        <el-form-item>
          <el-input
            ref="phone"
            v-model="forgetForm.phone"
            placeholder="请输入手机号"
            type="text"
            class="login-input"
            clearable
          />
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="14">
            <el-form-item>
              <el-input
                ref="phone"
                v-model="forgetForm.code"
                placeholder="请输入验证码"
                type="text"
                class="login-input"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-button style="position: relative;top: 4px;" @click="sendCode" type="primary">
              {{codeText}}
            </el-button>
          </el-col>
        </el-row>

        <el-form-item>
          <el-input
            ref="phone"
            v-model="forgetForm.newPassword"
            placeholder="请输入新密码"
            type="password"
            class="login-input"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-input
            ref="phone"
            v-model="confirmPass"
            placeholder="请再次输入密码"
            type="password"
            class="login-input"
            clearable
          />
        </el-form-item>

        <el-button type="primary" @click="updatePass">确认修改</el-button>
        <el-button type="primary" @click="$router.go(-1)">返回</el-button>

      </el-form>
    </div>
    <div class="float-header-logo"></div>
  </div>
</template>

<script>
import * as service from "@/views/login/service";
import md5 from "js-md5";

export default {
  name: "forgetPass",
  data() {
    return {
      forgetForm: {
        phone: '',
        code: '',
        newPassword: ''
      },
      confirmPass: '',
      intervalSeconds: 0,
      canSendSNSStatus: true,
      codeText: "发送验证码",
    }
  },
  methods: {
    sendCode() {
      if (!this.canSendSNSStatus) {
        return;
      }
      const params = {
        phone: this.forgetForm.phone,
        type: 1,
      };

      const target = this.$route.query.target
      if(target === 'platform') {
        params.userType = 4
      } else if(target === 'users') {
        params.userType = 6
      }

      if (params.phone === "") {
        this.$message.error("请输入手机号");
        return;
      }
      if (!/^1[3456789]\d{9}$/.test(params.phone)) {
        this.$message.error("请输入正确的手机号");
        return;
      }
      service.sendSmsCode(params).then((res) => {
        this.snsKey = res.data.key;
        this.$message.success("发送成功");
        this.canSendSNSStatus = false;
        this.startIntervalSecond();
      });
    },
    startIntervalSecond() {
      this.intervalSeconds = 60;
      let timer = setInterval(() => {
        this.intervalSeconds -= 1;
        this.codeText = this.intervalSeconds
        if (this.intervalSeconds <= 0) {
          this.canSendSNSStatus = true;
          this.codeText = '发送验证码'
          clearInterval(timer);
        }
      }, 1000);
    },

    updatePass() {
      const params = Object.assign({}, this.forgetForm)
      if (params.phone === "") {
        this.$message.error("请输入手机号");
        return;
      }
      if (!/^1[3456789]\d{9}$/.test(params.phone)) {
        this.$message.error("请输入正确的手机号");
        return;
      }

      if(this.confirmPass !== params.newPassword) {
        this.$message.error("再次输入密码和新密码不一致");
        return;
      }

      params.newPassword = md5(params.newPassword);

      const target = this.$route.query.target
      if(target === 'platform') {
        params.userType = '01'
      } else if(target === 'users') {
        params.userType = '00'
      }

      service.forgetPwd(params).then((res) => {
        this.$message.success('密码修改成功')
        this.$router.go(-1)
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .forget-pass-page{
    //padding: 15px;

    .background-img {
      position: absolute;
      z-index: -99;
      height: 100%;
      width: 100%;
    }
    .company-logo {
      position: absolute;
      left: 20%;
      top: 10%;
      display: flex;
      align-items: center;
      .logo-img {
        width: 100px;
        height: 100px;
      }
      .company-name {
        margin-left: 50px;
        font-size: 28px;
        color: rgb(22, 97, 141);
        font-weight: bold;
      }
    }
    .title-container {
      text-align: center;
      color: #909399;
      font-size: 14px;
      padding: 15px 0;
      display: flex;
      .title{
        width: 20%;
        cursor: pointer;
      }
      .actived {
        color: #00573e;
      }
    }
    .login-form {
      width: 300px;
      margin: 0 auto;
      .title-container {
        text-align: center;
        color: #00573e;
        font-size: 14px;
        padding: 0 0 30px 0;
      }
      .title-line {
        width: 100%;
        height: 1px;
        background: #eeeeee;
        margin-bottom: 25px;
      }
    }
    .title-line {
      width: 100%;
      height: 1px;
      background: #eeeeee;
      margin-bottom: 25px;
    }
    .login-bg {
      position: absolute;
      top: 250px;
      left: 50%;
      margin-left: -200px;
      width: 400px;
      height: auto;
      padding-bottom: 30px;
      border-radius: 10px;
      background: white;
    }
  }
  .title-container {
    text-align: center;
    color: #909399;
    font-size: 14px;
    padding: 15px 0;
    display: flex;
    .title{
      width: 50%;
      cursor: pointer;
    }
    .actived {
      color: #00573e;
    }
  }

  .confirm-update-button{
    width: 50%;
  }
</style>
